<?php 
$_SESSION[TINYMCE_UPLOAD] = 'upload/plugins/' . basename(dirname(__FILE__)) . '/';
if (!file_exists('../' . $_SESSION[TINYMCE_UPLOAD])) {
  Utils::create('../' . $_SESSION[TINYMCE_UPLOAD]);
}
$tags = unserialize(A_HCS);

$id = Utils::get_param('id', 0);
$opts = new stdClass();
$opts->cd = 't.id = :v1';
$opts->cdv = array(':v1' => $id);
$data = $orm->get('pb_ecom_article', NULL, NULL, NULL, NULL, $opts)->d;

$pg = Utils::get_param('pg', 1);
$opts = new stdClass();
$opts->cd = 't.tag_id LIKE :v1';
$opts->cdv = array(':v1' => '%,' . A_HC . ',%');
$objs = $orm->get('pb_ecom_article', 't.date', 'DESC', $pg, SIZE, $opts);
?>
<div class="pure-form pure-form-aligned" style="padding: 10px; width: 790px;">
  <legend>Courier</legend>

  <div class="pure-control-group">
    <label>Title</label>
    <input id="title" type="text" class="pure-input-1-2" placeholder="{New Article}" />
  </div>
  
  <div class="pure-control-group">
    <label>Category</label>
    <select id="tags"></select>
  </div>
  
  <div class="pure-control-group">
    <label>Content</label>
    <label><textarea id="tinymce"></textarea></label>
  </div>
  
  <div class="pure-control-group">
    <label>&nbsp;</label>
    <button class="pure-button pure-input-1-2 pure-button-primary" onclick="submit_obj();">Save</button>
  </div>
  
  <div class="pure-control-group expg">
    <label>&nbsp;</label>
    <button id="del_obj" class="pure-button" onclick="delete_obj();">Delete</button>
    <button id="new_obj" class="pure-button" onclick="__data = []; reset(); render();">New Article</button>
  </div>
</div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __tags = JSON.parse('<?php echo addslashes(json_encode($tags)); ?>');
  var __objs = JSON.parse('<?php echo addslashes(json_encode($objs)); ?>');
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __file = '<?php echo addslashes($app_data->param_file); ?>';
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __hc = '<?php echo A_HC; ?>';
  
  $(function(){
    reset();
    
    for (var idx in __tags) {
      var itm = __tags[idx];
      $('#tags').append('<option value="' + itm + '">' + itm+ '</option>');
    }
    
    $('#tinymce').val(__data[0].content);
    tinyMCE.init( {
      // General options
      mode: 'exact',
      elements: 'tinymce',
      theme: 'advanced',
      height: 500,

      plugins: 'filebrowser,style,iespell,preview,media,wordcount,inlinepopups',
      // Theme options
      theme_advanced_buttons1: 'undo,redo,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,|,forecolor,backcolor,|,image,media,|,preview',
      theme_advanced_buttons2: '',
      theme_advanced_buttons3: '',
      theme_advanced_toolbar_location: 'top',
      theme_advanced_toolbar_align: 'left',
      theme_advanced_statusbar_location: 'bottom',
      theme_advanced_resizing: true,

      file_browser_callback: 'filebrowser',

      theme_advanced_resize_horizontal: false,
      
      // Example content CSS (should be your site CSS)
      theme_advanced_font_sizes: '10px,11px,12px,13px,14px,15px,16px,17px,18px,19px,20px,21px,22px,23px,24px,25px,26px,27px,28px,29px,30px',
    });
    
    render();
  });

  function reset() {
    if (__data.length == 0) {
      __data[0] = {
        id: 0,
        title: '',
        tag_id: ',' + __hc + ',' + __tags[0] + ',',
        content: ''
      };
    }
  };

  function render() {
    Utils.leftbar(
      __objs,
      __data[0].id,
      'Help Center',
      'title',
      function(page) {
        Url.redirect(__redmd, __sys + '.php?i=' + __id + '&__file=' + __file + '&id=' + __data[0].id + '&pg=' + page);
      }
    );
    render_data();
  };
  
  function render_data() {
    Url.insert_param('id', __data[0].id);

    $('#title').val(__data[0].title);
    $('#tags').val(__data[0].tag_id.replace(__hc, '').replace(/,/g, ''));
    
    if (tinyMCE.get('tinymce')) {
      tinyMCE.get('tinymce').setContent(__data[0].content);
    } else {
      $('#tinymce').val(__data[0].content);
    }
    
    if (__data[0].id != 0) {
      $('#del_obj').show();
      $('#new_obj').show();
    } else {
      $('#del_obj').hide();
      $('#new_obj').hide();
    }
  };

  function submit_obj() {
    req = __data[0];
    req.title = $('#title').val();
    req.tag_id = ',' + __hc + ',' + $('#tags option:selected').val() + ',';
    req.content = tinyMCE.get('tinymce').getContent();
    req.__pg = __objs.p;

    Ajax.request(
      __requrl + 'submit_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        if (rslt.__objs) {
          __objs = rslt.__objs;
          delete rslt.__objs;
        }
        __data[0] = rslt;
        render();
      },
      null
    );
  };
  
  function delete_obj() {
    Ajax.request(
      __requrl.replace(__file, 'index') + 'delete_obj',
      'req=' + Utils.encodeURI(JSON.stringify(__data[0])),
      function(rslt) {
        Url.redirect(__redmd, __sys + '.php?i=' + __id);
      },
      { confirm: 1 }
    );
  };

  function get_obj(id) {
    var req = {
      id: id
    };
    Ajax.request(
      __requrl + 'get_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        render();
      },
      null
    );
    return false;
  };
  /*]]>*/
</script>